<?
require_once("mvc/action/Action.php");

class SaveAction extends Action {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("SaveAction");
    }

    public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        $errors = new ActionMessages();

        $default = $request->getContext()->getInitParameter(
            _Core::DEFAULT_LANGUAGE);

        $eventI18n =& $form->getEventI18n();
        $eventI18n->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));

        $event =& $eventI18n->getEvent();

        if($eventI18n->getLanguageCode() == $default) {

            $event->setDate($form->getYear()."-".$form->getMonth()."-".$form->getDay());

            // HACK FOR PROPEL. SET TO BOGUS VALUE FIRST THEN TO NULL.
            if($form->getHour() == "") {
                $event->setTime("00:00:00");
                $event->setTime(null);
            } else {
                $event->setTime($form->getHour().":".$form->getMinute().":00");
            }

        }

        $conn = Propel::getConnection();

        try {

            $conn->begin();

            // process image
            $formImage =& $form->getFormImage();

            if(!$form->getDeleteImage() && $formImage["size"] > 0) {

                $image = $event->getImageRelatedByImageId();
                $thumb = $event->getImageRelatedByThumbnailId();

                require_once("cms/core/util/ImageProperties.php");
                $properties = new ImageProperties(
                    $request->getContext()->getInitParameter(_Core::SETTING_LIST)
                );

                require_once("cms/core/util/ImageProcessor.php");
                ImageProcessor::process(
                    $conn, $errors, $formImage,
                    $properties, $image, $thumb
                );

                // Set the generated ids
                $event->setThumbnailId($thumb->getImageId());
                $event->setImageId($image->getImageId());

            } else if($form->getDeleteImage()) {

                $image = $event->getImageRelatedByImageId();
                $thumb = $event->getImageRelatedByThumbnailId();

                require_once("cms/core/om/BinaryFilePeer.php");

                BinaryFilePeer::doDelete($thumb->getBinaryFile(), $conn);
                BinaryFilePeer::doDelete($image->getBinaryFile(), $conn);
            }

            if($eventI18n->getEventI18nId() > 0) {

                EventI18nPeer::doUpdate($eventI18n, $conn);

                $event->setEventId($eventI18n->getEventId());
                EventPeer::doUpdate($event, $conn);

            } else {

                // An initial default language event is added.
                if(strlen($eventI18n->getEventId()) == 0) {
                    $eventI18n->setEventId(EventPeer::doInsert($event, $conn));
                    $eventI18n->setLanguageCode($default);
                }
                $eventI18n->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
                $eventI18n->setActive(_Core::AUTO_ACTIVATE);
                EventI18nPeer::doInsert($eventI18n, $conn);

            }

            $conn->commit();

        } catch(PropelException $e) {
            $conn->rollback();
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("error.save", true));
            $this->log->error($e->getMessage());
        }

        // Return any save errors.
        if(!$errors->isEmpty()) {
            $this->saveErrors($request, $errors);
            return $mapping->getInputForward();
        }

        $session  = $request->getSession();
        $listForm = $session->getAttribute("cms/eventListForm");
        $listForm->setPage(0);
        $session->setAttribute("cms/eventListForm", $listForm);

        return $mapping->findForward("success");

    }

}
?>